package com.vess.ratingweb.model;

import java.io.IOException;
import java.util.Calendar;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.util.Log;

public class JDBCAdapter {

	private final String NAMESPACE = "http://ratingweb.vess.com";
	private SoapObject request;
	private SoapSerializationEnvelope envelope;
	private HttpTransportSE androidHttpTransport;

	public JDBCAdapter(Context ctx) {
	}

	public void connectToWS(String METHOD_NAME, String URL) {
		request = new SoapObject(NAMESPACE, METHOD_NAME);
		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		androidHttpTransport = new HttpTransportSE(URL);
	}

	public void insertValueStringPropWS(String strNameFieldDB, String strValue) {
		// Pass value for variable of the web service
		PropertyInfo strVarProp = new PropertyInfo();
		strVarProp.setName(strNameFieldDB);
		strVarProp.setValue(strValue);
		strVarProp.setType(String.class);
		request.addProperty(strVarProp);
	}

	public void insertValueIntPropWS(String strNameFieldDB, int intValue) {
		// Pass value for variable of the web service
		PropertyInfo strVarProp = new PropertyInfo();
		strVarProp.setName(strNameFieldDB);
		strVarProp.setValue(intValue);
		strVarProp.setType(Integer.class);
		request.addProperty(strVarProp);
	}

	public String login(String strUsername, String strPassword) {
		String URL = "http://173.199.151.72:8408/WebJDBN/services/SelectDBRatingWeb?wsdl";
		String METHOD_NAME = "authenticationUser";
		connectToWS(METHOD_NAME, URL);
		// Pass value for Username variable of the web service
		insertValueStringPropWS("userName", strUsername);
		// Pass value for Password variable of the web service
		insertValueStringPropWS("password", strPassword);

		envelope.setOutputSoapObject(request);
		envelope.dotNet = true;

		String SOAP_ACTION = "http://ratingweb.vess.com/authenticationUser";
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			Log.i("ok", "1");
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			Log.i("ok", "2");
			return response.toString();
		} catch (IOException e) {
			Log.i("Error", e.getMessage());
		} catch (XmlPullParserException e) {
			Log.i("Error", e.getMessage());
		}

		return "-1";
	}

	public String selectUserData(int intUserId) {
		String URL = "http://173.199.151.72:8408/WebJDBN/services/SelectDBRatingWeb?wsdl";
		String METHOD_NAME = "selectUserDataDB";
		connectToWS(METHOD_NAME, URL);
		// Pass value for Username variable of the web service
		insertValueIntPropWS("intUserId", intUserId);

		envelope.setOutputSoapObject(request);
		envelope.dotNet = true;

		String SOAP_ACTION = "http://ratingweb.vess.com/selectUserDataDB";
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			return response.toString();
		} catch (IOException e) {
			Log.i("Error", e.getMessage());
		} catch (XmlPullParserException e) {
			Log.i("Error", e.getMessage());
		}

		return "-1";
	}

	public String selectSitesReviews(String strTimeStamp) {
		String URL = "http://173.199.151.72:8408/WebJDBN/services/SelectDBRatingWeb?wsdl";
		String METHOD_NAME = "selectSitesReviewsDB";
		connectToWS(METHOD_NAME, URL);
		// Pass value for Username variable of the web service
		insertValueStringPropWS("strTimeStamp", strTimeStamp);

		envelope.setOutputSoapObject(request);
		envelope.dotNet = true;

		String SOAP_ACTION = "http://ratingweb.vess.com/selectSitesReviewsDB";
		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			return response.toString();
		} catch (IOException e) {
			Log.i("Error", e.getMessage());
		} catch (XmlPullParserException e) {
			Log.i("Error", e.getMessage());
		}
		return "-1";
	}

	public String selectSites(String strSiteURL) {
		String URL = "http://173.199.151.72:8408/WebJDBN/services/SelectDBRatingWeb?wsdl";
		String METHOD_NAME = "selectSitesDB";
		connectToWS(METHOD_NAME, URL);
		// Pass value for Username variable of the web service
		insertValueStringPropWS("strSiteurl", strSiteURL);

		envelope.setOutputSoapObject(request);
		envelope.dotNet = true;

		try {
			String SOAP_ACTION = "http://ratingweb.vess.com/selectSitesDB";
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			return response.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "-1";
	}

	public String insertSitesReviewsDB(String strUsername, String strSite,
			String strReview, String strCusemail, String strTime,
			int intEvaluate, String strUser) {
		String URL = "http://173.199.151.72:8408/WebJDBN/services/SelectDBRatingWeb?wsdl";
		String METHOD_NAME = "insertSitesReviewsDB";
		connectToWS(METHOD_NAME, URL);

		// Pass value for userId variable of the web service
		insertValueStringPropWS("strUsername", strUsername);
		insertValueStringPropWS("strSite", strSite);
		insertValueStringPropWS("strReview", strReview);
		insertValueStringPropWS("strTime", strTime);
		insertValueStringPropWS("strCusemail", strCusemail);
		insertValueIntPropWS("intSendMail", 0);
		insertValueIntPropWS("intEvaluate", intEvaluate);
		insertValueStringPropWS("strUser", strUser);

		envelope.setOutputSoapObject(request);
		envelope.dotNet = true;

		try {
			Log.i("insert", "123");
			String SOAP_ACTION = "http://ratingweb.vess.com/insertSitesReviewsDB";
			androidHttpTransport.call(SOAP_ACTION, envelope);
			Log.i("insert", "456");
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			Log.i("insert", "789");
			return response.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "-1";
	}

	public String updateSitesReviewsDB(String strTime, String strURL) {
		String URL = "http://173.199.151.72:8408/WebJDBN/services/SelectDBRatingWeb?wsdl";
		String METHOD_NAME = "updateSitesReviewsDB";
		connectToWS(METHOD_NAME, URL);

		// Pass value for userId variable of the web service
		insertValueStringPropWS("strTime", strTime);
		insertValueStringPropWS("strURL", strURL);

		envelope.setOutputSoapObject(request);
		envelope.dotNet = true;

		try {
			String SOAP_ACTION = "http://ratingweb.vess.com/updateSitesReviewsDB";
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			return response.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "-1";
	}

	public String selectSitesUsers(String strUsername) {
		String URL = "http://173.199.151.72:8408/WebJDBN/services/SelectDBRatingWeb?wsdl";
		String METHOD_NAME = "selectSitesUsersDB";
		connectToWS(METHOD_NAME, URL);
		// Pass value for Username variable of the web service
		insertValueStringPropWS("strUser", strUsername);

		envelope.setOutputSoapObject(request);
		envelope.dotNet = true;

		try {
			String SOAP_ACTION = "http://ratingweb.vess.com/selectSitesUsersDB";
			androidHttpTransport.call(SOAP_ACTION, envelope);
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			return response.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "-1";
	}
}
